package com.yss.reportworld.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yss.common.core.constant.RequestDataHelper;
import com.yss.common.core.exception.BusinessException;
import com.yss.common.core.utils.StringUtils;
import com.yss.common.core.utils.uuid.IdUtils;
import com.yss.reportworld.domain.DemandBugInfo;
import com.yss.reportworld.mapper.DemandBugInfoMapper;
import com.yss.reportworld.util.TableInfoUtil;
import lombok.extern.slf4j.Slf4j;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.yss.reportworld.service.IDataDemandBugService;
import com.yss.reportworld.mapper.DataDemandBugMapper;
import com.yss.reportworld.domain.DataDemandBug;

/**
 * 数据和需求BUG关联关系Service业务层处理
 *
 * @author yss
 * @date 2023-03-27
 */
@Slf4j
@Service
public class DataDemandBugServiceImpl extends ServiceImpl<DataDemandBugMapper, DataDemandBug> implements IDataDemandBugService {
    @Autowired
    private DemandBugInfoMapper demandBugInfoMapper;

    /**
     * 查询数据和需求BUG关联关系列表
     *
     * @param dataDemandBug 数据和需求BUG关联关系
     * @return 数据和需求BUG关联关系
     */
    @Override
    public List<DemandBugInfo> selectDataDemandBugList(DataDemandBug dataDemandBug) {
        try {
            TableInfoUtil.handleTableVersion(dataDemandBug.getHisVersion()); // 替换表名
            Map<String, String> map = new HashMap<>();
            map.put("dataId", dataDemandBug.getDataId());
            map.put("dataCode", dataDemandBug.getDataCode());
            return demandBugInfoMapper.selectByPrimaryByDataId(map);
        } finally {
            RequestDataHelper.removeThreadLocal();
        }
    }

    @Override
    public DataDemandBug getInfoDemandBugInfo(DataDemandBug dataDemandBug) {
        try {
            TableInfoUtil.handleTableVersion(dataDemandBug.getHisVersion()); // 替换表名
            return this.getById(dataDemandBug.getId());
        } finally {
            RequestDataHelper.removeThreadLocal();
        }
    }

    @Override
    public void addDemandBugInfo(DataDemandBug dataDemandBug) {
        try {
            TableInfoUtil.handleTableVersion(dataDemandBug.getHisVersion()); // 替换表名
            dataDemandBug.setId(IdUtils.fastSimpleUUID());
            this.save(dataDemandBug);
        } finally {
            RequestDataHelper.removeThreadLocal();
        }
    }

    @Override
    public void editDemandBugInfo(DataDemandBug dataDemandBug) {
        try {
            TableInfoUtil.handleTableVersion(dataDemandBug.getHisVersion()); // 替换表名
            this.updateById(dataDemandBug);
        } finally {
            RequestDataHelper.removeThreadLocal();
        }
    }

    @Override
    public void deleteDemandBugInfo(DataDemandBug dataDemandBug) {
        try {
            if (StringUtils.isNull(dataDemandBug) || StringUtils.isEmpty(dataDemandBug.getIdList())) {
                throw new BusinessException("没有获取到要删除的修改记录信息！");
            }
            TableInfoUtil.handleTableVersion(dataDemandBug.getHisVersion()); // 替换表名
            this.removeByIds(dataDemandBug.getIdList());
        } finally {
            RequestDataHelper.removeThreadLocal();
        }
    }


    @Override
    public Map<String, List<String>> getDataDemandBugMap() {
        // 需求BUG code和数据，对应一个list
        QueryWrapper<DataDemandBug> wrapper = new QueryWrapper<>();
        List<DataDemandBug> dataDemandBugList = this.list(wrapper);
        if (StringUtils.isEmpty(dataDemandBugList)) {
            return null;
        }
        Map<String, List<String>> mapDataDemandBug = new HashMap<>();
        for (DataDemandBug demandBug : dataDemandBugList) {
            if (null == demandBug || StringUtils.isEmpty(demandBug.getDataId()) || StringUtils.isEmpty(demandBug.getDataCode())) {
                continue;
            }
            String key = demandBug.getDataCode() + "\t" + demandBug.getDataId();
            if (mapDataDemandBug.containsKey(key)) {
                List<String> list = mapDataDemandBug.get(key);
                list.add(demandBug.getDemandBugId());
                mapDataDemandBug.put(key, list);
            } else {
                List<String> list = new ArrayList<>();
                list.add(demandBug.getDemandBugId());
                mapDataDemandBug.put(key, list);
            }
        }
        return mapDataDemandBug;
    }

}
